package com.fortune.service.ananlysis;

import com.faneui.common.exception.BusinessException;
import com.fortune.service.ananlysis.dto.BillMailInfoDTO;
import com.fortune.service.ananlysis.dto.TradeRecordDTO;
import com.fortune.utils.AnanlysisUtils;
import com.google.common.collect.Lists;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

// 平安银行
@Service("pabCreditCardBillMailAnanlysis")
public class PabCreditCardBillMailAnanlysis extends AbstractAnanlysis {

    @Override
    void ananlysis(BillMailInfoDTO billMailInfoDTO, Document document) throws BusinessException {
        Elements es = document.select("strong:contains(账户信息)");
            Element e1 = es.first().parent().parent().parent().child(1);
        Elements es1 = e1.select("table").select("td");
        for (Element element1 : es1) {
            String str1 = element1.text();
            if ("本期账单日".equals(str1)
                    || "本期还款日".equals(str1)
                    || "信用额度".equals(str1)
                    || "取现额度".equals(str1)) {
                continue;
            }
            if (str1.contains("¥")) {
                str1 = str1.replace("¥", "").replace(",", "").trim();
                BigDecimal money = new BigDecimal(str1);
                AnanlysisUtils.setLimit(billMailInfoDTO, money);
            } else if (str1.length() == 10) {
                str1 = str1.replace("-", "").trim();
                AnanlysisUtils.setDate(billMailInfoDTO, str1);
            }
        }

        es1 = document.select("strong:contains(本期账单信息)").first().parent().parent().parent().child(1)
                .select("table").first()
                .select("table").get(2)
                .select("td");
        for (Element element1 : es1) {
            String str1 = element1.text();
            if (str1.contains("$")) {
                continue;
            }
            BigDecimal money = AnanlysisUtils.transferMoney(str1);
            AnanlysisUtils.setDebtMoney(billMailInfoDTO, money);
//            System.out.println(str1);
        }
        es = document.select("strong:contains(尊敬的)");
        String str = es.first().text();
        str = str.substring(3).replace(" 先生 ：", "").replace(" 女士 ：", "").trim();
        billMailInfoDTO.setCustName(str);

        es = document.select("td:contains(感谢您使用平安信用卡):contains(信用卡对账单)");
        str = es.first().text();
        str = str.substring(str.indexOf("以下是您") + 4, str.indexOf("月")).trim();
        billMailInfoDTO.setBillMonth(str);

        es = document.select("td:contains(万里通积分)").select("[width=133]");
        str = es.first().parent().parent().child(2).child(0).text();
        billMailInfoDTO.setPoint(new BigDecimal(str));

        es = document.select("td:contains(********)").select("[colspan=4]");
        str = es.first().text();
        billMailInfoDTO.setCardName(str.substring(0, str.indexOf("-")).trim());
        billMailInfoDTO.setCardCode("*" + str.substring(str.indexOf("********") + 8, str.indexOf("********") + 12).trim());


        es1 = es.first().parent().parent().select("tr");
        List<TradeRecordDTO> recordDTOList = Lists.newArrayList() ;
        for (int i = 2; i<es1.size(); i++) {
            str = es1.get(i).text() ;
            System.out.println("--------------------"+str);
            if(str.contains("其他")) {
                break;
            }
            TradeRecordDTO tradeRecordDTO = new TradeRecordDTO();
            tradeRecordDTO.setTradeDate(es1.get(i).child(0).text().replace("-",""));
            tradeRecordDTO.setBookDate(es1.get(i).child(1).text().replace("-",""));
            tradeRecordDTO.setMerchantName(es1.get(i).child(2).text());
            tradeRecordDTO.setMoney(AnanlysisUtils.transferMoney( es1.get(i).child(3).text()));
            recordDTOList.add(tradeRecordDTO);
        }
        billMailInfoDTO.setTradeRecordDTOS(recordDTOList);
    }



}
